package com.videoaggre.zcl.parse.impl

import com.videoaggre.zcl.R
import com.videoaggre.zcl.api.ApiHelper
import com.videoaggre.zcl.api.QIMAN_SERVICE
import com.videoaggre.zcl.bean.*
import com.videoaggre.zcl.db.*
import com.videoaggre.zcl.utils.Logger
import com.videoaggre.zcl.utils.MoshiUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup

object QimanParse {
    fun getQimanBrowse(worksId: String, serieId: String) = "${ApiHelper.QIMAN}${worksId}/${serieId}.html"

    @JvmStatic
    suspend fun parseHome(html: String?) = withContext(Dispatchers.IO) {
        val mQimanHome = QimanHome()
        try {
            val rootElement = Jsoup.parse(html)
            rootElement.select(".banner a").forEach {
                mQimanHome.banner.add(QimanBanner(it.attr("href").replace("/", ""), it.selectFirst("img").attr("data-src")))
            }
            rootElement.select(".floor").forEachIndexed { index, element ->
                val titleElement = element.selectFirst("a")
                mQimanHome.item.add(QimanComicHomeTitle(R.mipmap.ic_home_dj, titleElement.selectFirst("span").text(), 11))
                element.select(".comic-item").forEachIndexed { aIndex, aElement ->
                    mQimanHome.item.add(
                        QimanComicHomeNormal(
                            aElement.selectFirst("a").attr("href").replace("/", ""),
                            aElement.selectFirst("img").attr("data-src"),
                            aElement.selectFirst(".comic-name").text(),
                            aElement.selectFirst(".comic-tip").text()
                        )
                    )
                }
            }
            mQimanHome
        } catch (e: Exception) {
            Logger.e(e)
            mQimanHome
        }
    }

    @JvmStatic
    suspend fun parseDetail(id: String, html: String?) = withContext(Dispatchers.IO) {
        val mQimanComicDetail = QimanComicDetail()
        try {
            mQimanComicDetail.id = id
            val rootElement = Jsoup.parse(html)
            rootElement.selectFirst(".comic-info-box").also {
                mQimanComicDetail.img = it.selectFirst("img").attr("src")
                mQimanComicDetail.name = it.selectFirst("h1").text()
                it.select("p").forEachIndexed { index, element ->
                    element.text()?.also {
                        when {
                            it.contains("作者") -> mQimanComicDetail.author = element.text()
                            it.contains("类型") -> mQimanComicDetail.type = element.text()
                            it.contains("更新时间") -> mQimanComicDetail.updateTime = element.text()
                            it.contains("最新章节") -> mQimanComicDetail.latestChapter = element.text()
                        }
                    }
                }
            }
            mQimanComicDetail.desc = rootElement.selectFirst(".comic-intro").text()
            rootElement.select(".catalog-list .chapter-item").forEach {
                mQimanComicDetail.chapters.add(QimanComicChapter(it.attr("data-id"), it.text()))
            }
            mQimanComicDetail.chapters.addAll(MoshiUtils.parseArray(QIMAN_SERVICE.bookchapter(id) ?: "[]", QimanComicChapter::class.java) ?: mutableListOf())
            rootElement.select(".comic-recommend").last().select(".rec-item").forEach {
                mQimanComicDetail.recommends.add(
                    QimanComicHomeNormal(
                        it.selectFirst("a").attr("href").replace("/", ""),
                        it.selectFirst("img").attr("data-src"),
                        it.selectFirst(".comic-name").text()
                    )
                )
            }
            mQimanDao.addQimanComicHistory(QimanComicHistory(id, mQimanComicDetail.img, mQimanComicDetail.name, mQimanComicDetail.desc))
            mQimanComicDetail
        } catch (e: Exception) {
            Logger.e(e)
            mQimanComicDetail
        }
    }

    @JvmStatic
    fun parseComicBrowse(mSerieId: String, html: String?): QimanComicBrowse {
        val qimanComicBrowse = QimanComicBrowse(mSerieId)
        try {
            val rootElement = Jsoup.parse(html)
            rootElement.select(".comic-page img").takeIf { it.size > 0 }?.also { qimanComicBrowse.qimanComicPre = mutableListOf() }?.forEach {
                qimanComicBrowse.qimanComicPre?.add(it.attr("data-original"))
            }
            val hrefs = rootElement.selectFirst(".next a").attr("href").split("/")
            if (hrefs.size >= 3) {
                qimanComicBrowse.nextId = hrefs[2].replace(".html", "")
            }
        } catch (e: Exception) {
            Logger.e(e)
        }
        return qimanComicBrowse
    }

    @JvmStatic
    fun parseSearch(html: String?): MutableList<QimanComicHomeNormal> {
        val qimanComicHomeNormals = mutableListOf<QimanComicHomeNormal>()
        try {
            val rootElement = Jsoup.parse(html)
            rootElement.select(".comic-list-item").forEach {
                qimanComicHomeNormals.add(
                    QimanComicHomeNormal(
                        it.selectFirst("a").attr("href").replace("/", ""),
                        it.selectFirst("img").attr("src"),
                        it.selectFirst(".comic-name").text(),
                        it.selectFirst(".comic-update-at").text()
                    )
                )
            }
        } catch (e: Exception) {
            Logger.e(e)
        }
        return qimanComicHomeNormals
    }
}